using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WPCoco.Tasks
{
    public class PerFrameAnimation : TargetedTask<ITextured>
    {
        public PerFrameAnimation(TextureFrame[] frames, float duration)
            : base(duration)
        {
            this.frames = frames;
        }

        TextureFrame[] frames = new TextureFrame[0];

        public override void InitializeWithTarget(ITextured target)
        {
            
        }

        public override void UpdateWithTarget(ITextured target, float time)
        {
            int index = (int)((time * frames.Length) / Duration);
            if (index >= frames.Length) index = frames.Length - 1; if (index < 0) index = 0;
            target.SetTextureFrame(frames[index]);
        }

        public override Task Clone()
        {
            return new PerFrameAnimation(frames.Clone() as TextureFrame[], Duration);
        }

        public override Task Reverse()
        {
            TextureFrame[] clones = new TextureFrame[frames.Length];
            for (int i = 0; i < clones.Length; i++)
                clones[i] = frames[clones.Length - i - 1];
            return new PerFrameAnimation(clones, Duration);
        }
    }
}
